<template>
  <div class="">
    <Common-Form
      ref="commonForm"
      :flag="flag"
      :table-id="tableId"
      did-deep
    />
  </div>
</template>

<script type="text/ecmascript-6">
import CommonForm from '@/components/CommonForm';
export default {
  components: { CommonForm },

  props: {
    tableId: {
      type: Number,
      default: 0
    },

    isShow: {
      type: Boolean,
      default: false
    },

    outData: {
      type: Object,
      default: () => ({})
    },
    flag: {
      type: String,
      default: '0'
    }
  },

  inject: ['form', 'sourceData'],

  watch: {
    isShow: {
      handler: function(v) {
        if (v) {
          this.$nextTick(() => {
            this.initFormValue();
          });
        }
      },

      immediate: true
    }
  },

  methods: {
    initFormValue() {
      const form = this.form();
      const sourceData = this.sourceData();
      const {
        form: formRead
      } = sourceData;
      const { $props } = this.outData;
      const formNow = formRead[sourceData.tableId];
      const reaioSelected = formNow.checkbox[0] || formNow.radioSelectRow;
      // const reaioSelected = formRead[tableId].radioSelectRow;
      // const title = $props.renderKey.title;
      // 取buttonCode,不要取title
      const buttonCode = $props.renderKey.buttonCode;
      // const statusMap = {
      //   编辑: 0,
      //   添加: 1
      // };
      // const status = statusMap[title];
      // 1添加,3编辑,20查看
      if (buttonCode === 3) {
        this.assignFormHeaderDeep(form[sourceData.tableId].headers, reaioSelected);
      } else if (buttonCode === 1) {
        this.assignFormHeaderDeep(form[sourceData.tableId].headers, {});
      } else if (buttonCode === 20) {
        this.assignFormHeaderDeep(form[sourceData.tableId].headers, $props.dbTableRowInfo);
      }
    },

    // 深度遍历赋值
    assignFormHeaderDeep(data, asdignValue) {
      if (Array.isArray(data)) {
        data.forEach(item => {
          this.assignFormHeaderDeep(item, asdignValue);
        });
      } else {
        if (data.children && data.children.length) {
          this.assignFormHeaderDeep(data.children, asdignValue);
        } else {
          if (data.dataIndex in asdignValue) {
            data.myValue = asdignValue[data.dataIndex];
          } else {
            data.myValue = '';
          }
        }
      }
    },

    formInstance() {
      return this.$refs.commonForm;
    },

    init() {
      const commonForm = this.formInstance();
      commonForm.initData();
    },

    // 提交
    submit() {
      const form = this.formInstance();
      let result = false;
      form
        .submit()(valid => {
          if (valid) {
            const data = form.$data.originData.headers.reduce((prev, curr) => {
              prev[curr.dataIndex] = curr.myValue;
              return prev;
            }, {});
            result = this.collectData(data);
            // result = this.collectData(form.formData);
          } else {
            this.$message.warning('请检查数据填写是否正确');
            return false;
          }
        });
      return result;
    },

    clearData() {
      this.formInstance().resetDataEmpty();
    },

    collectData(data) {
      const requestData = {};
      for (const key in data) {
        requestData[key] = data[key];
        // requestData[key] = data[key].myValue;
      }

      return requestData;
    }
  }
};
</script>
